package com.tic.robot.core;

import android.bluetooth.BluetoothSocket;

import com.tic.robot.utils.ToastUtil;

import java.io.IOException;
import java.io.OutputStream;

/**
 * Created by chuangjiewang on 2016/10/8.
 */

public class BluetooshManager {

    private static BluetooshManager sManager;

    private BluetoothSocket mSocket = null; // 客户端socket

    private BluetooshManager() {

    }

    public static BluetooshManager getInstance() {
        if (sManager == null) {
            synchronized (BluetooshManager.class) {
                if (sManager == null) {
                    sManager = new BluetooshManager();
                }
            }
        }

        return sManager;
    }

    public BluetoothSocket getSocket() {
        return mSocket;
    }

    public void setSocket(BluetoothSocket mSocket) {
        this.mSocket = mSocket;
    }

    public void closeSocket() {
        if (this.mSocket == null) {
            try {
                this.mSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    // 发送数据
    public void sendMessageHandler(String msg) {
        if (mSocket == null) {
            ToastUtil.showMsg("没有可用的连接");
            return;
        }
        try {
            OutputStream os = mSocket.getOutputStream();
            os.write(msg.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
